/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package android.isisufes.util;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBHelper {
	private static final String TAG = "MenuProvider";
    public void onCreate(SQLiteDatabase db) {
        try {
			db.execSQL("CREATE TABLE IF NOT EXISTS activeInvitations (id INTEGER,"
			        + "campName TEXT," + "authorName TEXT," + "desc TEXT,"
			        + "expDate TEXT, status INTEGER, user TEXT NOT NULL , public TEXT, PRIMARY KEY (id, user)" + ");");
		} catch (SQLException e) {
			Log.d("DEBUG","db 1 error");
			//database already exists, do nothing.
		}
		
		try {
			db.execSQL("CREATE TABLE IF NOT EXISTS activeCamp (id INTEGER,"
			        + "lat INTEGER," + "lon INTEGER," + "radius INTEGER," +
			         "user TEXT NOT NULL, campName TEXT NOT NULL, authorName TEXT, "+
			         "desc TEXT, expDate TEXT, status INTEGER NOT NULL, public INTEGER,  PRIMARY KEY (id, user));");
		} catch (SQLException e) {
			Log.d("DEBUG","db 2 error");
			//database already exists, do nothing.
		}
		
		try {
			db.execSQL("CREATE TABLE IF NOT EXISTS communities (id INTEGER,"
			        + "name TEXT," + "owner TEXT ," + "desc TEXT," +
			         "user TEXT, PRIMARY KEY (id,user));");
		} catch (SQLException e) {
			Log.d("DEBUG","db 2 error");
			//database already exists, do nothing.
		}
		
		try {
			db.execSQL("CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT,"
			        + "msg TEXT," + "sender TEXT ," + 
			         "user TEXT);");
		} catch (SQLException e) {
			Log.d("DEBUG","db 2 error");
			//database already exists, do nothing.
		}
		
		try {
			db.execSQL("CREATE TABLE IF NOT EXISTS fileInfo (idattach TEXT, userid TEXT,"
			        + "campaigncreator TEXT,size TEXT,campaignname TEXT," +
			         "filename TEXT,status TEXT,type TEXT, hash TEXT, desc TEXT, _id INTEGER PRIMARY KEY AUTOINCREMENT);");
		} catch (SQLException e) {
			Log.d("DEBUG","db 2 error");
			//database already exists, do nothing.
		}
		
		try {
			db.execSQL("CREATE TABLE IF NOT EXISTS fileContent (idattach TEXT, id TEXT, content TEXT);");
		} catch (SQLException e) {
			Log.d("DEBUG","db 2 error");
			//database already exists, do nothing.
		}
		
		try {
			db.execSQL("CREATE TABLE IF NOT EXISTS contacts (id INTEGER,"
			        + "name TEXT," + "user TEXT, PRIMARY KEY (id, user));");
		} catch (SQLException e) {
			Log.d("DEBUG","db 2 error");
			//database already exists, do nothing.
		}
		
		
		try {
			db.execSQL("CREATE TABLE IF NOT EXISTS localdata (latitude INTEGER, longitude INTEGER);");
		} catch (SQLException e) {
			Log.d("DEBUG","db 2 error");
		}
		
		Cursor query = db.query("localdata", null, null, null, null, null, null);
		if (query.getCount() == 0) {
			db.execSQL("INSERT into localdata (latitude, longitude) values (0,0);");
		}
		query.close();
		
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS activeInvitations");
        onCreate(db);
    }
}
